(*****************************************************************************
 * Delphi Plug-in Framework (DPF)
 * Copyright (C) 2006-2007 Tao Yuan
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the
 * License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.                                                               }
 *
 * The Initial Developer of the Original Code is Tao Yuan. Portions created by
 * Tao Yuan are Copyright (C) Tao Yuan (taoy.cn att gmail dott com)
 * All rights reserved.
 *****************************************************************************)
 
unit RDResourceManager;

interface

uses
  Rcl;

type
  TResourceManager = class(TObject)
  private
    bundles: IStrMap;
    function processParams(str: string; data: array of string): string; overload;
    function processParams(str: string; data: IStrCollection): string; overload;
    function processParams(str: string; data: string): string; overload;
    function replaceAll(str, sFrom, sTo: string): string;
  public
    constructor Create;
    function getMessage(packageName, messageKey, locale: string; data: array of
        string): string; overload;
    function getMessage(packageName, messageKey, locale: string): string; overload;
    function getMessage(packageName, messageKey: string; data: array of string):
        string; overload;
    function getMessage(packageName, messageKey: string): string; overload;
  end;

var
  ResourceManager: TResourceManager;

implementation

uses
  SysUtils;

constructor TResourceManager.Create;
begin
  bundles := TStrHashMap.Create(16, False);
end;

function TResourceManager.getMessage(packageName, messageKey, locale: string;
    data: array of string): string;
begin
//  Object obj = bundles.get(packageName + '|' + locale);
//  if (obj == null) {
//      try {
//          obj = ResourceBundle.getBundle(packageName + ".Resources", //$NON-NLS-1$
//                  locale);
//      } catch (MissingResourceException mre) {
//          obj = FAKE_BUNDLE;
//      }
//      bundles.put(packageName + '|' + locale, obj);
//  }
//  if (obj == FAKE_BUNDLE) {
//      return "resource " + packageName + '.' + messageKey //$NON-NLS-1$
//          + " not found for locale " + locale; //$NON-NLS-1$
//  }
//  try {
//      String result = ((ResourceBundle) obj).getString(messageKey);
//      return (data == null) ? result : processParams(result, data);
//  } catch (MissingResourceException mre) {
//      return "resource " + packageName + '.' + messageKey //$NON-NLS-1$
//          + " not found for locale " + locale; //$NON-NLS-1$
//  }
  Result := 'resource ' + packageName + '.' + messageKey //$NON-NLS-1$
          + ' not found for locale ' + locale;
end;

function TResourceManager.getMessage(packageName, messageKey, locale: string):
    string;
begin
  Result := getMessage(packageName, messageKey, locale, []);
end;

function TResourceManager.getMessage(packageName, messageKey: string; data:
    array of string): string;
begin
  Result := getMessage(packageName, messageKey, 'zh', data);
end;

function TResourceManager.getMessage(packageName, messageKey: string): string;
begin
  Result := getMessage(packageName, messageKey, []);
end;

function TResourceManager.processParams(str: string; data: array of string):
    string;
var
  I: Integer;
begin
  result := str;
  for I := 0 to Length(data) - 1 do
    result := replaceAll(result, '{' + IntToStr(I) + '}', '' + data[i]); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
end;

function TResourceManager.processParams(str: string; data: IStrCollection):
    string;
var
  it: IStrIterator;
  I: Integer;
begin
  it := data.First;
  I := 0;
  while it.HasNext do
  begin
    Result := replaceAll(result, '{' + IntToStr(I) + '}', it.next);
    Inc(I);
  end;

end;

function TResourceManager.processParams(str: string; data: string): string;
begin
  Result := replaceAll(result, '{0}', '' + data);
end;

function TResourceManager.replaceAll(str, sFrom, sTo: string): string;
var
  p: Integer;
begin
  result := str;
  p := 0;
  while (true) do
  begin
      p := Rcl.Str.Find(sFrom, result, p);
      if (p = 0) then
          break;
          
      result := Rcl.Str.SubString(result, 1, p - 1) + sTo
          + Rcl.Str.SubString(result, p + Length(sFrom));
      p := p + Length(sTo);
  end;
end;

initialization
  ResourceManager := TResourceManager.Create;

finalization
  try
    ResourceManager.Free;
    ResourceManager := nil;
  finally
  end;
end.
